package entities

import "errors"

var (
	NoSuchBookException = errors.New("the book is not enough")
)

type Book struct {
	Name       string
	Total      int
	Author     string
	CreateTime string
}

func CreateBook(name string, total int, author, createTime string) (bookP *Book) {
	bookP = &Book{
		Name:       name,
		Total:      total,
		Author:     author,
		CreateTime: createTime,
	}
	return
}

func (bookP *Book) canBrrow(count int) int {

	return bookP.Total - count
}

func (bookP *Book) Borrow(number int) (err error) {
	if bookP.canBrrow(number) < 0 {
		err = NoSuchBookException
		return
	}
	bookP.Total -= number
	return
}
